var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()


Page({
  data: {
    currentTab: 0, // 当前选中的tab
    tabList: [{ name: '待查看', type: 1 }, { name: '已查看', type: 2 }], // tab
    activeNames: [0],
    ticket_num:'',
    company:'',
    factory:'',
    pageList:[],
    equip:[],
    id:0,
    info:null,
  },
  onLoad: function (options) {
  
    this.setData({
      ticket_num: options.ticket_num || '',
      company: options.company || '',
      factory: options.factory || '',
      id: options.id || '',
      info:JSON.parse(options.info)  || '',
    })
    console.log(this.data.info)
    // console.log("==================================")
    if(options.equip!=null && options.equip!='' && options.equip!=undefined){
      // console.log("++++++++++++++++++++++++++++")
      var equips=new Array();
      var str=options.equip;
      equips=str.split(",");
      this.setData({
        equip:equips
      })
      wx.setNavigationBarTitle({
        title: '巡检设备' 
      })
    }

    this.getRecord() // 巡检记录
  },

  getRecord() { // 巡检记录
    var params = {
      ticket_num: this.data.ticket_num,
      company: this.data.company,
      factory: this.data.factory,
    }
    wx.showLoading();
    var that=this;
    api.$get(api.pollingManage.record, params).then((res) => { 
      var dataList=new Array();
      console.log(that.data.equip);
      var equips=that.data.equip;
      var index=0;
      if(equips!=null && equips.length!=0){
        for(var i=0 ;i<equips.length;i++){
          for(var e in res.data){
                if(res.data[e].equipment==equips[i]){
                  dataList[index]=res.data[e];
                  index++;
                }
            }
        }
       if(dataList.length==0){
          this.setData({
            pageList: res.data,
          })
       }else{
         this.setData({
             pageList: dataList
         })
       }
      }else{
          this.setData({
            pageList: res.data,
          })
      }
console.log('pageList',this.data.pageList)
      wx.hideLoading()
    })
  },

  toRecordFile(e){ // 去查看档案
    var { equipment} = $.detail(e)
    $.linkurl(`/pages/pollingManage/pollingManageLookFile/pollingManageLookFile?equipment=${equipment}&company=${this.data.company}&factory=${this.data.factory}`)
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  toPollingManageContentHandle(e){ // 去巡检内容
    var {item} = $.detail(e)
    $.linkurl(`/pages/pollingManage/pollingManageContent/pollingManageContent?id=${this.data.id}&ticket_num=${this.data.ticket_num}&equipment=${item.equipment}`)
  },
  toPollingManageCheckInHandle(e) { // 去登记缺陷
    var { item } = $.detail(e)
    console.log(item);
    var info=this.data.info.info;
    var company=this.data.info.company;
    var leader=this.data.info.leader;
    var staff=this.data.info.staff;
    var info1={
      dept_id:info.dept_id || '',
      equip:item.equipment||'',
      ticket_num: info.ticket_num ||'', //检修和巡检独有
      company: info.company || '',
      company_id: info.company_id || '',
      factory: info.factory ||  '',
      factory_id: info.factory_id ||  '',
      source:'巡检'
    }
    $.linkurl(`/pages/defectManage/defectManageFlawRegister/defectManageFlawRegister?info=${JSON.stringify(info1)}&type=5`)
  },
  // },

  toDefectRecordHandle(e){ // 去缺陷记录
    var { equipment, type } = $.detail(e)
    var ticket_num = '' // 巡检订单号【本次需解决要传，全部未解决不传】
    if(type ==1){ // 本次解决
      ticket_num = this.data.ticket_num
    } else if(type==2){// 全部未解决
      
    }
    $.linkurl(`/pages/pollingManage/pollingManageDefectRecord/pollingManageDefectRecord?equipment=${equipment}&company=${this.data.company}&factory=${this.data.factory}&ticket_num=${ticket_num}`)
  },
  toPollingManageInspectionRecord(){ // 去继续巡检
    $.linkurl(`/pages/pollingManage/pollingManageInspectionRecordDetails/pollingManageInspectionRecordDetails?id=${this.data.id}&ticket_num=${this.data.ticket_num}`)
  }

})